import java.util.Scanner;

public class demo03 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int[][] arr=new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j]=sc.nextInt();
            }
        }
        int[][] arr1=new int[n][n];
        func1(arr,arr1,n);
        int[][] arr2=new int[n][n];
        for (int i = 0; i < m; i++) {
            func1(arr1,arr2,n);
            arr2=func2(arr,arr1,arr2,n);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(arr2[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void func1(int[][] arr,int[][] arr1,int n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr1[i][j]=arr[i][j];
            }
        }
    }
    public static int[][] func2(int[][] arr,int[][] arr1,int[][] arr2,int n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int sum=0;
                for (int k = 0; k < n; k++) {
                    sum+=arr[i][k]*arr1[k][j];
                }
                arr2[i][j]=sum;
            }
        }
        return arr2;
    }
}
